package com.bristua.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author hrt
 * @date 2018年1月4日
 * @description 日期工具类
 */
public class DateUtils {

    public static final SimpleDateFormat ALL_DATETIME_FORMAT = new SimpleDateFormat("yyyyMMddHHmmsssss");
    public static final SimpleDateFormat SHOW_DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");
    public static final SimpleDateFormat SIMPLE_DATETIME_FORMAT = new SimpleDateFormat("yyMMddHHmmsssss");
    public static final SimpleDateFormat DATETIME_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
    public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
    public static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HHmmss");
    public static final String SHOW_DATETIME_FORMAT_GENERAL = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前时间
     * 格式：yyyyMMddHHmmss
     *
     * @return
     */
    public static String getDateTime() {
        return DATETIME_FORMAT.format(new Date());
    }

    /**
     * 获取当前时间
     * 格式：yyyy-MM-dd HH:mm:ss sss
     *
     * @return
     */
    public static String getShowDateTime() {
        return SHOW_DATETIME_FORMAT.format(new Date());
    }

    /**
     * 获取当前日期
     * 格式：yyyyMMddHHmmss
     *
     * @return
     */
    public static String getDate() {
        return DATE_FORMAT.format(new Date());
    }

    /**
     * 获取时间戳
     *
     * @return
     */
    public static String getTimeStamp() {
        return String.valueOf(new Date().getTime());
    }

    /**
     * 获取昨天格式为：yyyyMMdd
     *
     * @return
     */
    public static String getYesterDate() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - 1);
        return DATE_FORMAT.format(cal.getTime());
    }

    /**
     * yyyyMMdd转换为yyyy年MM月dd日
     *
     * @param date
     * @return
     */
    public static String convertDate(String date) {

        SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
        try {
            return sf.format(DATE_FORMAT.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * yyyyMMddHHmmss转换为yyyy-MM-dd HH:mm:ss
     *
     * @param datetime
     * @return
     */
    public static String convertDateTime(String datetime) {

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sf.format(DATETIME_FORMAT.parse(datetime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 使用参数Format格式化Date成字符串
     *
     * @return String
     */
    public static String format(Date date, String pattern) {
        return date == null ? "" : new SimpleDateFormat(pattern).format(date);
    }


    /**日期与当前日期相减获得秒数
     * @param startDate
     * @return
     */
    public static int calLastedTime(Date startDate) {
        long a = new Date().getTime();
        long b = startDate.getTime();
        int c = (int) ((a - b) / 1000);
        return c;
    }


    public static Date stringToDate(String date){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d= null;
        try {
            d = sf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }
}
